<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: relative;
            width: 400px;
            /* height: 40px; */
            margin: 150px auto;
            border-bottom: 1px solid #ccc;
            overflow: hidden;
        }

        img {
            position: absolute;
            /* border:solid 1px red; */
            width: 30px;
            right: 0;
            top: -2px;
        }

        input {
            outline: none;
            border: none;
            width: 370px;
        }
    </style>
</head>

<body>
    <div>
        <img src="./img/jdclose.png" alt="">
        <input type="password" name="password" id="pwd">
    </div>
    <script>
        var img = document.querySelector('img');
        var pwd = document.querySelector('#pwd');
        // 相关算法：利用一个flag变量，来判断flag的值，如果是1，就切换成文本框，并且flag被设置为0
        // 如果flag是0就切换成密码框，flag设置为1
        var flag = 0;
        img.onclick = function () {
            if (flag == 0) {
                pwd.type = 'text';
                img.src = './img/jdopen.png';
                flag = 1;
            }else{
                pwd.type = 'password';
                img.src = './img/jdclose.png';
                flag = 0;
            }

        }
    </script>
</body>
<script>

</script>

</html>